const { Comment } = require("../model");

// 改
exports.updateSquare = async (req, res, next) => {
  try {
    const fun = await Comment.findByIdAndUpdate(req.params.id, {
      $set: req.body
    })
    if (fun) {
      res.status(200).json({
        code: 200,
        msg: "success",
      });
    } else {
      res.status(200).json({
        code: 500,
        msg: "修改失败",
      });
    }
    
  } catch (error) {
    next(error);
  }
};

// 查
exports.getSquare = async (req, res, next) => {
  try {
    // find({name:/程序员/}) 
    const items = await Comment.find();
    res.status(200).json({
      code: 200,
      data: items,
    });
  } catch (error) {
    next(error);
  }
};
// 增
exports.addSquare = async (req, res, next) => {
  try {
    const model = new Comment(req.body);
    await model.save();
    res.status(200).json({
      code: 200,
      msg: "success",
    });
  } catch (error) {
    next(error);
  }
};
// 删
exports.deleteSquare = async (req, res, next) => {
  try {
    console.log('deleteSquare', req.body.id)
    const rs = await Comment.findByIdAndDelete(req.body.id)
    if (rs) {
      res.status(200).json({
        code: 200,
        msg: '删除成功'
      });
    }
  } catch (error) {
    next(error);
  }
};
exports.getSquareById = async (req, res, next) => {
  try {
    const rs = await Comment.find({
      squareId: req.params.id
    })
    if (rs) {
      res.status(200).json({
        code: 200,
        data: rs
      });
    }
  } catch (error) {
    next(error);
  }
};




